/* Begin KR - C3 */

#include "OtherKR.h"
#include <fstream>
#include <iostream>

/* Will write category and product from vector to the file */
void OtherKR::writeProducts(string const& path, vector<Category> const& categories, vector<Product> const& products)
{
        //Check validity first
        for(int i=0;i<products.size();i++)
        {
                Product p = products.at(i);
                if (!isProductValid(p)) {
                        cout << "Product " <<  p.id_get() << " is not valid. New products writing failed." << endl;     
                        return;
                }
        }

        //Init file
        ofstream file;
        file.open(path.c_str());

        //If file is open 
        if (file.is_open())
        {
                int categoriesSize = categories.size();
                file << categoriesSize << "\n";

                for(int i=0;i<categoriesSize;i++)
                {
                        Category c = categories.at(i);
                        file << c.id_get() << " " << c.name_get() << "\n";
                }

                file << "\n\n";

                int productSize = products.size();
                file << productSize << "\n";

                for(int i=0;i<productSize;i++)
                {
                        Product p = products.at(i);
                        file << p.id_get() << " " << p.cat_get() << " " << p.name_get() << " " << p.price_get() << "\n";
                }

                //Done, close file
                file.close();
        }
}

/* Will write customer data from vector to the file */
void OtherKR::writeCustomers(string const& path, vector<Customer> const& customers)
{
        //Init file
        ofstream file;
        file.open(path.c_str());

        //If file is open 
        if (file.is_open())
        {
                int custSize = customers.size();
                file << custSize << "\n";

                for(int i=0;i<custSize;i++)
                {
                        Customer c = customers.at(i);
                        file << c.getID() << " " << c.getName() << " " << c.getAddress().addr_no << " " << c.getAddress().addr_name << " " << c.getAddress().addr_postal << "\n";
                }

                //Done, close file
                file.close();
        }
}

/* Will write cart and cart item data from vector to the file */
void OtherKR::writeCarts(string const& path, vector<Cart> const& carts, vector<Cart_item> const& items, vector<Product> const& products)
{
        //Check validity first
        for(int i=0;i<carts.size();i++)
        {
                Cart c = carts.at(i);
                if (!isCartValid(c, items)) {
                        cout << "Cart " <<  c.cart_get() << " is not valid. New carts writing failed." << endl; 
                        return;
                }
        }

        for(int i=0;i<items.size();i++)
        {
                Cart_item item = items.at(i);
                if (!isCartItemValid(item, products)) {
                        cout << "Cart item " <<  item.item_get() << " is not valid. New carts writing failed." << endl; 
                        return;
                }
        }

        //Init file
        ofstream file;
        file.open(path.c_str());

        //If file is open 
        if (file.is_open())
        {
                //Insert cart size
                int cartSize = carts.size();
                file << carts.size() << "\n";
        
                //Insert cart data
                for(int i=0;i<cartSize;i++)
                {
                        //Insert
                        Cart cart = carts.at(i);
                        file << cart.cart_get() << " " << cart.cust_get() << " " << cart.Date_get().year() << " " << cart.Date_get().month() << " " << cart.Date_get().day() << "\n";
                }

                file << "\n";

                //Insert cart item data
                int itemSize = items.size() - 1;
                file << itemSize << "\n";
        
                //Insert item data
                for(int i=0;i<=itemSize;i++)
                {
                        //Insert
                        Cart_item item = items.at(i);
                        file << item.item_get() << " " << item.cart_get() << " " << item.prod_get() << " " << item.qty_get() << "\n";
                }

                //Done, close file
                file.close();
        }
}

/* Check whether a cart item refer to existing product or not */
bool OtherKR::isCartItemValid(Cart_item item, vector<Product> products)
{
        int id = item.prod_get();
        int size = products.size();

        for(int i=0;i<size;i++) 
        { ;
                if ( products.at(i).id_get() == id) return true;
        }

        return false;
}

/* Check whether a cart has at least one cart item */
bool OtherKR::isCartValid(Cart cart, vector<Cart_item> items)
{
        int id = cart.cart_get();
        int size = items.size();

        for(int i=0;i<size;i++) 
        {
                if (items.at(i).cart_get() == id) return true;
        }

        return false;
}

/* Check whether the product price is less than zero */
bool OtherKR::isProductValid(Product prod)
{
        return (prod.price_get()>=0);
}

/* End KR - C3 */
